package com.bilibili.servlet;

import java.io.IOException;
import java.util.Collections;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.dto.VideoAndCategory;
import com.bilibili.entity.CommonResult;
import com.bilibili.entity.Page;
import com.bilibili.entity.User;
import com.bilibili.service.CategoryService;
import com.bilibili.service.VideoAndCategoryService;
import com.bilibili.service.impl.CategoryServiceImpl;
import com.bilibili.service.impl.VideoAndCategoryServiceImpl;

@SuppressWarnings("serial")//抑制serialVersionUID字段警告
@WebServlet("/queryMyAllSendVideo")
public class QueryMyAllSendVideoServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json; charset=utf-8");
		VideoAndCategoryService vacs = new VideoAndCategoryServiceImpl();
		CategoryService cs = new CategoryServiceImpl();
		VideoAndCategory vac = new VideoAndCategory();
		
		int user_id = ((User) request.getSession().getAttribute("loginUser")).getUserId();
		int pageNum = 1,pageSize = 5;
		vac.setUserId(user_id);
		
		if(request.getParameter("pageNum") != null){
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		if(request.getParameter("pageSize") != null){
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		
		String state = request.getParameter("state");
		if(state != null && !"".equals(state)) {
			if(state.equals("已发布")) {
				vac.setStatus(1);
			}else if(state.equals("审核中")){
				vac.setStatus(0);
			}else {
				vac.setStatus(-1);
			}
		}else {
			vac.setStatus(-1);
		}
		
		String sortOrder = request.getParameter("sortOrder");
		if(sortOrder != null && !"".equals(sortOrder)) {
			if(sortOrder.equals("排序：最新发布")) {
				vac.setSortOrder("create_time");
			}else if(sortOrder.equals("播放量最高")) {
				vac.setSortOrder("view_count");
			}else if(sortOrder.equals("点赞最多")) {
				vac.setSortOrder("like_count");
			}
		}
		
		String title = request.getParameter("title");
		vac.setTitle(title);
		
		String category = request.getParameter("category");
		if(category != null && !"".equals(category) && !"全部分区".equals(category)) {
			int category_id = cs.selectCategoryIdByName(category).getCategoryId();
			vac.setCategoryId(category_id);
		}else {
			vac.setCategoryId(-1);
		}
		
		Page<VideoAndCategory> vacPage = vacs.selectMyAllsendVideo(vac, pageNum, pageSize);
		CommonResult cr = new CommonResult();
		if(sortOrder.equals("评论最多")) {
			Collections.sort(vacPage.getData());
			cr.setCode(200);
			cr.setData(vacPage);
			response.getWriter().write(cr.toJSONString());
			return;
		}
		if(vacPage.getData().size() != 0) {
			cr.setCode(200);
			cr.setData(vacPage);
			response.getWriter().write(cr.toJSONString());
		}else {
			cr.setCode(409);
			response.getWriter().write(cr.toJSONString());
		}
	}
}
